Skip to content

Conversation

raonitimo
Copy link

@raonitimo raonitimo commented Sep 7, 2025

Summary

This PR adds a new PostgreSQL database backend that integrates the AWS Advanced Python Wrapper to provide automatic failover capabilities for Amazon RDS clusters while maintaining comprehensive Prometheus metrics collection.

Features

  • Automatic failover handling for AWS RDS clusters using the AWS Advanced Python Wrapper
  • Enhanced Prometheus metrics specifically for AWS failover events
  • Query retry mechanism on successful failover with proper session state management
  • Configurable AWS wrapper options including plugins and timeout settings
  • Seamless integration with existing django-prometheus functionality and metrics

New Metrics

This backend introduces three new Prometheus metrics:

  • django_db_aws_failover_success_total - Counter of successful database failovers
  • django_db_aws_failover_failed_total - Counter of failed database failovers
  • django_db_aws_transaction_resolution_unknown_total - Counter of transactions with unknown resolution status

Usage

DATABASES = {
    'default': {
        'ENGINE': 'django_prometheus.db.backends.postgresql_aws',
        'HOST': 'database.cluster-xyz.us-east-1.rds.amazonaws.com',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'PORT': '5432',
        'OPTIONS': {
            'aws_plugins': 'failover,host_monitoring',  # AWS wrapper plugins
            'connect_timeout': 30,  # Connection timeout in seconds
            'socket_timeout': 30,   # Socket timeout in seconds
        },
    }
}

Dependencies

This backend requires the AWS Advanced Python Wrapper:

pip install aws-advanced-python-wrapper

The backend gracefully handles missing dependencies by raising ImproperlyConfigured with clear installation instructions.

Implementation Details

  • Code Style: Formatted with Ruff according to project standards
  • Error Handling: Comprehensive error handling for all AWS failover scenarios
  • Logging: Detailed logging for operational visibility
  • Testing: Unit tests included with proper dependency mocking
  • Documentation: Updated backend README with usage examples and best practices

Test Plan

  • Unit tests for backend functionality and error handling
  • Integration tests with mocked AWS dependencies
  • Full django-prometheus test suite passes
  • Code formatting and linting with Ruff
  • Backward compatibility verified - no regressions in existing functionality

Benefits

This enhancement enables django-prometheus users to:

  1. Monitor RDS failover events with detailed Prometheus metrics
  2. Achieve better uptime through automatic failover handling
  3. Maintain observability during database cluster transitions
  4. Configure failover behavior based on application requirements

The implementation follows the existing patterns in django-prometheus for database backends and maintains full compatibility with the current API.

This commit adds a new PostgreSQL database backend that integrates the AWS
Advanced Python Wrapper to provide automatic failover capabilities for Amazon
RDS clusters while maintaining comprehensive Prometheus metrics collection.

Key features:
- Automatic failover handling for AWS RDS clusters
- Enhanced Prometheus metrics for AWS-specific events
- Query retry on successful failover
- Configurable AWS wrapper plugins and timeouts
- Seamless integration with existing django-prometheus functionality

The backend adds three new metrics:
- django_db_aws_failover_success_total
- django_db_aws_failover_failed_total
- django_db_aws_transaction_resolution_unknown_total

Usage:
    DATABASES = {
        'default': {
            'ENGINE': 'django_prometheus.db.backends.postgresql_aws',
            'OPTIONS': {
                'aws_plugins': 'failover,host_monitoring',
                'connect_timeout': 30,
                'socket_timeout': 30,
            },
        }
    }

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant